<?php
/*
  wallypark_buy.php


Author: Mark Montano
Date    : 10-17-09

This page is to process the purchase of the parking packages from the LAX WallyPark 
car wash page.

The Process will follow this flow:

- set up the session
- set up a shopping cart
- add the item to the shopping cart
- check to see if the user has an account and if they do then give them a login screen


- if the user does not have an account then create an account for them 
	- connect the session shopping cart to the customer basket
    - send the user to the payment page
*/


// bring in the oscommerce configurations
require('includes/application_top.php');


// redirect the customer to a friendly cookie-must-be-enabled page if cookies are disabled (or the session has not started)
if ($session_started == false) {
  tep_redirect(tep_href_link(FILENAME_COOKIE_USAGE));
}

//  here $cart is the current shopping cart
// a session is already started once the user gets to this page
// lets add the product to the cart

if (is_numeric($_POST["package"])) {
// continue only if the package submitted is a number

	//add the product

	$cart->add_cart(cleanInput($_POST["package"]));
	
	// check to see if the user has an account already with this email address
	$usercheck = tep_db_query("select * from customers where customers_email_address='".sanitize($_POST["email"])."'");
	if (tep_db_num_rows($usercheck) <= 0) {
	// it is ok to create a new account here
		$firstname = cleanInput($_POST["first_name"]);
		$lastname = cleanInput($_POST["last_name"]);
		$email = cleanInput($_POST["email"]);
		$phone = cleanInput($_POST["phone"]);
		$address = cleanInput($_POST["address"]);
		$city = cleanInput($_POST["city"]);
		$state = cleanInput($_POST["state"]);
		$zip = cleanInput($_POST["zipcode"]);
		
		
		// create the customer record
		 $sql_data_array = array('customers_firstname' => $firstname,
                              'customers_lastname' => $lastname,
                              'customers_email_address' => $email,
                              'customers_telephone' => $phone,
                              'customers_password' => tep_encrypt_password($email.$phone));

     	if (ACCOUNT_GENDER == 'true') $sql_data_array['customers_gender'] = '-';
     // if (ACCOUNT_DOB == 'true') $sql_data_array['customers_dob'] = tep_date_raw($dob);

      	tep_db_perform(TABLE_CUSTOMERS, $sql_data_array);
		
		$customerid = tep_db_insert_id();
		
		// get the tax zone
		$zoneid = tep_db_query("select zone_id from zones where zone_country_id = 223 and zone_code='".$state."'");
		$zoneid = tep_db_fetch_array($zoneid);
		
		
		$sql_data_array = array('customers_id' => $customerid,
                              'entry_firstname' => $firstname,
                              'entry_lastname' => $lastname,
                              'entry_street_address' => $address,
                              'entry_postcode' => $zip,
                              'entry_city' => $city,
                              'entry_country_id' => 223,
							  'entry_gender' => '-',
							  'entry_zone_id'=> $zoneid["zone_id"],
							  'entry_state' => $state);
 
      
      tep_db_perform(TABLE_ADDRESS_BOOK, $sql_data_array);

	  $addressid = tep_db_insert_id();
		
	  // update the customers record with the address id
	  tep_db_query("update customers set customers_default_address_id=".$addressid." where customers_id=".$customerid);
		
	  if (SESSION_RECREATE == 'True') {
        tep_session_recreate();
      }
      $customer_id = $customerid;
      $customer_first_name = $firstname;
      $customer_default_address_id = $addressid;
      $customer_country_id = 223;
      $customer_zone_id = $zoneid["zone_id"];
      tep_session_register('customer_id');
      tep_session_register('customer_first_name');
      tep_session_register('customer_default_address_id');
      tep_session_register('customer_country_id');
      tep_session_register('customer_zone_id');

      tep_db_query("update " . TABLE_CUSTOMERS_INFO . " set customers_info_date_of_last_logon = now(), customers_info_number_of_logons = customers_info_number_of_logons+1 where customers_info_id = '" . (int)$customerid . "'");
	  // restore cart contents
      $cart->restore_contents();
      
      tep_session_register('shipping');
      
      
      // send the user to the payment page
      tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT));
      exit;
	}
	else {
	// presumably this user already has an account.  We will send them to the login page.
		 tep_redirect(tep_href_link(FILENAME_LOGIN));
		 exit;
	}
}
else {
// no product to add to just send them to th home page of the ste
	header("Location: /index.html");
	exit;
}


?>



